package com.baizhi.controller.user;

import com.baizhi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("register")
    public String register(String useName,String password,String password1) {
        System.out.println("useName = " + useName);
        boolean register = userService.register(useName,password,password1);
        if (register) {
            return "redirect:/user/login.jsp";
        }else
            return "redirect:/user/register.jsp";
    }
    @RequestMapping("login")
    public String login(String username, String pwd1, String validateCode, HttpSession session){
        System.out.println("username = " + username);
        String realCode = (String) session.getAttribute("realCode");
        if(realCode.equals(validateCode) !=true){
            return "redirect:/user/login.jsp";
        }
        boolean login = userService.login(username, pwd1);
        if(login){
            //用于在展示页面显示名称
            session.setAttribute("login",username);
            return "redirect:/person/show.do";
        }else
            return "redirect:/user/login.jsp";
    }
}
